package reader

import (
	"regexp"
	"github.com/Tang-RoseChild/mahonia"
	"strings"
)

// 将字符串装换为UTF-8模式
func ConvertUTF8(date string, charset string) string {
	dec := mahonia.NewDecoder(charset)
	date = dec.ConvertString(date)
	return date
}

// 分析
func (doc *document) Analyzer(reg string) []string {
	r := regexp.MustCompile(reg)
	return r.FindAllString(doc.data, -1)
}

// 根据前后缀来提取有效信息
func (doc *document) Reg(prefix, suffix string) []string {
	name := prefix + `.*` + suffix
	submit := doc.Analyzer(name)
	for k, v := range submit {
		submit[k] = v[len(prefix):len(v)-len(suffix)]
	}
	return submit
}

// 去除前后缀数据
func NotStr(data, prefix, suffix string) string {
	i := strings.Index(data, prefix)
	if i < 0 {
		return data
	}
	j := strings.Index(data[i:], suffix)
	if j < 0 {
		return data
	}
	j += i + len(suffix)
	data = data[:i] + data[j:]
	if strings.Index(data, prefix) < strings.Index(data, suffix) {
		data = NotStr(data, prefix, suffix)
	}
	return data
}
